package school.xauat.offershow.serice.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import school.xauat.offershow.mapper.UserMapper;
import school.xauat.offershow.pojo.User;
import school.xauat.offershow.pojo.dto.EntryResponseType;
import school.xauat.offershow.pojo.dto.UserResponseType;
import school.xauat.offershow.serice.EntryService;
import school.xauat.offershow.serice.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zsy
 * @date ：Created 2021/12/19 20:12
 * @description：
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    EntryService entryService;

    @Override
    public User checkUser(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        List<User> users = userMapper.selectByMap(map);
        return users.size() == 0 ? null : users.get(0);
    }

    @Override
    public String checkUsername(String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        List<User> users = userMapper.selectByMap(map);
        return users.size() == 0 ? null : users.get(0).getUsername();
    }

    @Override
    public int add(User user) {
        return userMapper.insert(user);
    }

    @Override
    public UserResponseType getUserInfo(int user_id, String order) {
        UserResponseType user = new UserResponseType();
        user.setId(user_id);
        List<EntryResponseType> owns = entryService.searchByUserId(user_id, order);
        List<EntryResponseType> stars = entryService.selectByUserId(user_id, order);
        user.setStarsEntryLists(stars);
        user.setOwnEntryList(owns);
        return user;
    }
}
